package com.newbe.kaptcha.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.newbe.kaptcha.util.PhotoCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: Newbe
 * @Date: 2020/4/4 12:25
 * @Description:
 */
@Controller
public class KaptchaController {

   @Autowired
    private DefaultKaptcha defaultKaptcha;

    @GetMapping("/defaultKaptcha")
    public String defaultKaptcha( HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception{
        return PhotoCodeUtil.createCode(defaultKaptcha,httpServletRequest, httpServletResponse);
    }

    @RequestMapping("/imgvrifyDefaultKaptcha")
    public String imgvrifyDefaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Boolean codeFlag = PhotoCodeUtil.checkCode(httpServletRequest, httpServletResponse);
        if (codeFlag==true){
            return "success";
        }
        else return "index";
    }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }
}
